package com.ruoyi.eval.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.eval.domain.EvalQuestion;
import com.ruoyi.eval.mapper.EvalQuestionMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.eval.mapper.EvalPaperMapper;
import com.ruoyi.eval.domain.EvalPaper;
import com.ruoyi.eval.service.IEvalPaperService;

/**
 * 评价试卷Service业务层处理
 * 
 * @author yepanpan
 * @date 2025-05-06
 */
@Service
public class EvalPaperServiceImpl implements IEvalPaperService 
{
    private static final Logger log = LoggerFactory.getLogger(EvalPaperServiceImpl.class);

    @Autowired
    private EvalPaperMapper evalPaperMapper;
    @Autowired
    private EvalQuestionMapper questionMapper;

    /**
     * 查询评价试卷
     * 
     * @param id 评价试卷主键
     * @return 评价试卷
     */
    @Override
    public EvalPaper selectEvalPaperById(Long id)
    {
        return evalPaperMapper.selectEvalPaperById(id);
    }

    /**
     * 查询评价试卷列表
     * 
     * @param evalPaper 评价试卷
     * @return 评价试卷
     */
    @Override
    public List<EvalPaper> selectEvalPaperList(EvalPaper evalPaper)
    {
        return evalPaperMapper.selectEvalPaperList(evalPaper);
    }

    /**
     * 新增评价试卷
     * 
     * @param evalPaper 评价试卷
     * @return 结果
     */
    @Override
    public int insertEvalPaper(EvalPaper evalPaper)
    {
        evalPaper.setCreateTime(DateUtils.getNowDate());
        return evalPaperMapper.insertEvalPaper(evalPaper);
    }

    /**
     * 复制评价试卷
     *
     * @param evalPaper 评价试卷
     * @return 结果
     */
    @Override
    public int copyEvalPaper(EvalPaper evalPaper){
        Long oldId = evalPaper.getId();
        evalPaper.setCreateTime(DateUtils.getNowDate());
        evalPaper.setId(null);
        int ret = evalPaperMapper.insertEvalPaper(evalPaper);

        //试题和选项
        EvalQuestion qc = new EvalQuestion();
        qc.setPaperId(oldId);
        List<EvalQuestion> questions = questionMapper.selectEvalQuestionList(qc);
        for(EvalQuestion question:questions){
            EvalQuestion evalQuestion = questionMapper.selectEvalQuestionById(question.getId());
            evalQuestion.setId(null);
            evalQuestion.setPaperId(evalPaper.getId());
            questionMapper.insertEvalQuestion(evalQuestion);
            if(StringUtils.isNotEmpty(evalQuestion.getEvalQuestionOptionList())){
                evalQuestion.getEvalQuestionOptionList().forEach(o->{
                    o.setId(null);
                    o.setQuestionId(evalQuestion.getId());
                });
                questionMapper.batchEvalQuestionOption(evalQuestion.getEvalQuestionOptionList());
            }
        }

        return ret;
    }

    /**
     * 修改评价试卷
     * 
     * @param evalPaper 评价试卷
     * @return 结果
     */
    @Override
    public int updateEvalPaper(EvalPaper evalPaper)
    {
        evalPaper.setUpdateTime(DateUtils.getNowDate());
        return evalPaperMapper.updateEvalPaper(evalPaper);
    }

    /**
     * 批量删除评价试卷
     * 
     * @param ids 需要删除的评价试卷主键
     * @return 结果
     */
    @Override
    public int deleteEvalPaperByIds(Long[] ids)
    {
        return evalPaperMapper.deleteEvalPaperByIds(ids);
    }

    /**
     * 删除评价试卷信息
     * 
     * @param id 评价试卷主键
     * @return 结果
     */
    @Override
    public int deleteEvalPaperById(Long id)
    {
        return evalPaperMapper.deleteEvalPaperById(id);
    }


}
